########################################################################
# upstatus.py - GUI code to show wickddup's upload status windows.
#
# Created 2006.04.29 by Warren Young
#
# Copyright (c) 2006 by Pixel Corps.  This program may be used under
# the terms of the X11 license, a copy of which should have accompanied
# this program, in the LICENSE file.
########################################################################

import localparms, mainframe

import wx

import os, thread


#### class UploadStatusWindow ##########################################

class UploadStatusWindow(mainframe.MainFrame):
	""" 
	Class defining the window managing user feedback for the upload
	status.
	""" 
	def __init__(self, uploader):
		# Call parent class to handle basics common to all wickddup
		# top-level windows.
		mainframe.MainFrame.__init__(self, uploader)

		# Init member variables
		self.lastFile = -1
		self.percent = 0
		self.progress = None

		# Create a panel to hold all the GUI bits
		self.panel = wx.Panel(self)

		# Create the contents of the panel, and lay it out with sizers
		numFiles = len(self.uploader.localFiles)
		if numFiles == 1: plural = ''
		else: plural = 's'
		self.statusText1 = wx.StaticText(self.panel, wx.ID_ANY,
				"Uploading %d file%s to" % (numFiles, plural),
				style = wx.ALIGN_CENTRE)
		self.statusText2 = wx.StaticText(self.panel, wx.ID_ANY,
				"sftp://%s@%s:%d/%s..." % (self.uploader.userName,
				self.uploader.hostName, self.uploader.port,
				self.uploader.RemoteDir()), style = wx.ALIGN_CENTRE)
		self.innerSizer = wx.BoxSizer(wx.VERTICAL)
		self.innerSizer.Add(self.statusText1, 1, border = 2,
				flag = wx.EXPAND | wx.TOP | wx.BOTTOM | wx.ALIGN_BOTTOM)
		self.innerSizer.Add(self.statusText2, 1, border = 2,
				flag = wx.EXPAND | wx.TOP | wx.BOTTOM | wx.ALIGN_TOP)
		self.outerSizer = wx.BoxSizer(wx.VERTICAL)
		self.outerSizer.Add(self.innerSizer, 1, flag = wx.ALL,
				border = 12)
		self.SetAutoLayout(True)
		self.panel.SetSizerAndFit(self.outerSizer)
		self.SetClientSize(self.outerSizer.GetSize())

		# Now that we know the frame size, place it 1/3 up the screen,
		# horizontally centered.
		screenX, screenY = wx.GetDisplaySize()
		winX, winY = self.GetSize()
		self.Move((screenX / 2 - winX / 2, screenY / 3 - winY / 2))

		# Start progress bar update timer, ticking once per second
		self.timer = wx.Timer(self)
		self.Bind(wx.EVT_TIMER, self.OnUpdateProgress, self.timer)
		self.timer.Start(1000)

		# Start SFTP uploader thread
		thread.start_new_thread(self.uploader.ThreadEntry, ())


	def OnUpdateProgress(self, timer):
		if self.progress and self.percent > 0 and \
				not self.progress.Update(min(self.percent, 100)):
			self.uploader.CloseTransport()


	def RecreateProgressDialog(self, fileNum):
		if self.progress:
			self.progress.Destroy()

		self.progress = wx.ProgressDialog(
				'%s Progress' % localparms.parms.programName,
				'Uploading %s (file %d of %d)...' %
				(self.uploader.localFiles[fileNum], fileNum + 1,
				len(self.uploader.localFiles)), maximum = 100,
				parent = self, style = wx.PD_CAN_ABORT |
				wx.PD_REMAINING_TIME | wx.PD_ELAPSED_TIME)
		self.progress.CenterOnScreen()


	def UpdateProgress(self, fileNum, percent):
		if fileNum != self.lastFile:
			self.lastFile = fileNum
			self.RecreateProgressDialog(fileNum)
		self.percent = percent

